/*
 * @Description: Echarts图表模型js
 * @Author: Huang Junjie
 * @Date: 2021-06-03 09:18:25
 * @LastEditTime: 2021-06-03 16:03:58
 * @LastEditors: Huang Junjie
 */
// 引入echarts
// const echarts = require('echarts')

// 当月远动缺陷统计 - 饼状图
export function PieChart01(myChart) {
  myChart.clear();
  myChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: 50,
      right: 80,
      itemWidth: 18,
      itemHeight: 10,
      itemGap: 25,
      orient: 'vertical',
      textStyle: {
        color: '#ffffff',
        fontSize: '14px'
      },
      formatter: function(name) {
        // 获取legend显示内容
        const data = [
          { value: 40, name: '电力', itemStyle: { color: '#eaad1a' }},
          { value: 20, name: '变电', itemStyle: { color: '#02c2bb' }},
          { value: 19, name: '网开关', itemStyle: { color: '#3695ff' }}
        ];
        // let total = 0
        let tarValue = 0;
        for (let i = 0, l = data.length; i < l; i++) {
          // total += data[i].value
          if (data[i].name === name) {
            tarValue = data[i].value;
          }
        }
        // const p = (tarValue / total * 100).toFixed(2)
        return name + ' ' + ' ' + tarValue + '件';
      }
    },
    series: [
      {
        clockwise: true,
        center: ['30%', '50%'],
        name: '设置检修覆盖率（年）',
        type: 'pie',
        radius: ['35%', '70%'],
        avoidLabelOverlap: true,
        label: {
          normal: {
            show: true,
            position: 'inner',
            textStyle: {
              fontSize: '14px',
              color: '#0c265f'
            },
            formatter: '{c}件'
          }
        },
        labelLine: {
          show: true
        },
        data: [
          { value: 40, name: '电力', itemStyle: { color: '#eaad1a' }},
          { value: 20, name: '变电', itemStyle: { color: '#02c2bb' }},
          { value: 19, name: '网开关', itemStyle: { color: '#3695ff' }}
        ]
      }
    ]
  });

  // 解决自适应
  setTimeout(function() {
    window.addEventListener('resize', () => {
      myChart.resize();
    });
  }, 500);
}
